package com.sl.core.workflow.listener;

import cn.hutool.core.util.ObjectUtil;
import com.sl.core.engine.trans.ProcessInstance;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.pipeline.IExecutionFinishedListener;
import org.apache.hop.pipeline.PipelineMeta;
import org.apache.hop.pipeline.engine.IPipelineEngine;

public class WorkflowPipelineListener implements IExecutionFinishedListener<IPipelineEngine<PipelineMeta>> {

    public static final WorkflowPipelineListener INSTANCE = new WorkflowPipelineListener();

    public static IWorkFlowStatusService SERVICE = null;

    @Override
    public void finished(IPipelineEngine<PipelineMeta> engine) throws HopException {
        if (ObjectUtil.isNull(SERVICE)) {
            return;
        }
        if (engine instanceof ProcessInstance p) {
            Integer end = p.isEnd();
            if (ObjectUtil.equals(end, 0)) {
                return;
            }
            if (ObjectUtil.equals(end, 2)) {
                SERVICE.finish(p.getInstanceId());
            } else {
                SERVICE.error(p.getInstanceId());
            }

        }
    }
}
